package com.jensen.service.base;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jensen.mapper.base.BaseMapper;

import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;
import java.util.List;

/**
 * Created by Jensen on 2016/10/25.
 */
public abstract class BaseServiceImpl<T,ID extends Serializable> implements BaseService<T,ID> {

    public abstract BaseMapper<T, ID> getMapper() ;

    public T save(T entity) {
        return getMapper().save(entity);
    }

    public void delete(ID id) {
        getMapper().delete(id);
    }

    public T findOne(ID id) {
        return getMapper().findOne(id);
    }

    public T update(T entity) {
        return getMapper().update(entity);
    }

    public List<T> findAll(){
        return getMapper().findAll();
    }

    public PageInfo getPageRs(int pageNum, int pageSize, String pageQueryM,Object... args){
        try {
            PageHelper.startPage(pageNum,pageSize,"id");

            List list= (List) this.getMapper().getClass().getMethod(pageQueryM).invoke(getMapper(),args);
            PageInfo pageInfo=new PageInfo(list);
            return pageInfo;
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
        return null;
    }

}
